package com.cntaiping.royal.bass.core.controller;

import com.cntaiping.royal.bass.core.entity.JClassRecord;
import com.cntaiping.royal.bass.core.entity.ModelData;
import com.cntaiping.royal.bass.core.entity.RequestPO.JClassRecordDTO;
import com.cntaiping.royal.bass.core.entity.ResponseVO.ClassRecordVO;
import com.cntaiping.royal.bass.core.service.ClassRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangwei
 * @since 20/2/11
 */
@RestController
@RequestMapping("/classRecord")
public class ClassRecordController {
    @Autowired
    private ClassRecordService classRecordService;

    @PostMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public Object addClassRecord(@RequestBody JClassRecordDTO jClassRecordDTO) {

        Map map = new HashMap<String, Object>();
        map.put("systemId", jClassRecordDTO.getSystemId());
        map.put("courseId", jClassRecordDTO.getCourseId());
        map.put("openid", jClassRecordDTO.getOpenid());

        //去掉一天只能打一次卡现在
//        List<JClassRecord> classRecords = classRecordService.selectRecords(map);

        ModelData modelData = new ModelData();

//        if(CollectionUtils.isEmpty(classRecords)){

        if (jClassRecordDTO != null) {
            JClassRecord classRecord = new JClassRecord();

            classRecord.setTitle(jClassRecordDTO.getTitle());
            classRecord.setCourseId(jClassRecordDTO.getCourseId());
            classRecord.setOpenid(jClassRecordDTO.getOpenid());
            classRecord.setContent(jClassRecordDTO.getContent());
            classRecord.setSystemId(jClassRecordDTO.getSystemId());
            classRecord.setNikeName(jClassRecordDTO.getNikeName());
            classRecord.setIconeUrl(jClassRecordDTO.getIconeUrl());

            if (!CollectionUtils.isEmpty(jClassRecordDTO.getImgs())) {
                String imgs = StringUtils.join(jClassRecordDTO.getImgs(), ",");
                classRecord.setImgs(imgs);
            }

            if (!CollectionUtils.isEmpty(jClassRecordDTO.getVideos())) {
                String videos = StringUtils.join(jClassRecordDTO.getVideos(), ",");
                classRecord.setVideos(videos);
            }

            int count = classRecordService.addClassRecord(classRecord);

            if (count > 0) {
                modelData.setSuccess(true);
                modelData.setMsg("插入成功");
            } else {
                modelData.setSuccess(false);
                modelData.setMsg("插入失败");
            }
        }

//        }else {
//            modelData.setSuccess(false);
//            modelData.setMsg("该课程已经打过卡");
//        }

        modelData.setjData("");

        return modelData;
    }

    @GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public Object queryList(@RequestParam(value = "systemId", required = false) String systemId,
                            @RequestParam(value = "courseId", required = false) Integer courseId,
                            @RequestParam(value = "openid", required = false) String openid,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {

        ModelData modelData = new ModelData();

        Map map = new HashMap<String, String>();
        map.put("systemId", systemId);
        map.put("courseId", courseId);
        map.put("openid", openid);
        map.put("pageStart", (pageNum - 1) * pageSize);
        map.put("pageSize", pageSize);

        List<JClassRecord> classRecords = classRecordService.queryList(map);

        List<ClassRecordVO> classRecordVOs = new ArrayList<>();

        modelData.setSuccess(true);

        if (CollectionUtils.isEmpty(classRecords)) {
            modelData.setMsg("查询结果为空");
        } else {

            for (JClassRecord jClassRecord : classRecords) {

                ClassRecordVO classRecordVO = new ClassRecordVO();

                if(jClassRecord != null){
                    String strImg = jClassRecord.getImgs();
                    String strVideo = jClassRecord.getVideos();

                    BeanUtils.copyProperties(jClassRecord, classRecordVO);

                    if(!StringUtils.isEmpty(strImg)){
                        String[] imgs = strImg.split(",");
                        List<String> imgList = Arrays.asList(imgs);
                        classRecordVO.setImgs(imgList);
                    }

                    if(!StringUtils.isEmpty(strVideo)) {
                        String[] videos = strVideo.split(",");
                        List<String> videoList = Arrays.asList(videos);
                        classRecordVO.setVideos(videoList);
                    }


                }

                classRecordVOs.add(classRecordVO);
            }
            modelData.setMsg("查询成功");
            modelData.setjData(classRecordVOs);

        }

        return modelData;
    }

    @GetMapping(value = "participateCount", produces = {"application/json;charset=UTF-8"})
    public Object participateCount(@RequestParam(value = "systemId", required = false) String systemId) {

        ModelData modelData = new ModelData();

        Map countMap = classRecordService.queryParticpateCount(systemId);

        if (countMap != null) {
            modelData.setSuccess(true);
            modelData.setjData(countMap);
            modelData.setMsg("查询成功");
        }

        return modelData;
    }
}
